﻿using System;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;
using System.Xml;
using System.Collections;
namespace l1cs.server.model.npc.action
{
    public class L1NpcTeleportAction : L1NpcXmlAction
    {
        private L1Location _loc;
        private int _heading;
        private int _price;
        private bool _effect;

        public L1NpcTeleportAction()
        {
        }

        public L1NpcTeleportAction( XmlReader reader )
            : base( reader )
        {
            Hashtable attribute = L1NpcXmlParser.getAllAttribute( reader );
            int x = L1NpcXmlParser.getIntAttribute( (string)attribute["X"], -1 );
            int y = L1NpcXmlParser.getIntAttribute( (string)attribute["Y"], -1 );
            int mapId = L1NpcXmlParser.getIntAttribute( (string)attribute["Map"], -1 );
            _loc = new L1Location( x, y, mapId );

            _heading = L1NpcXmlParser.getIntAttribute( (string)attribute["Heading"], 5 );

            _price = L1NpcXmlParser.getIntAttribute( (string)attribute["Price"], 0 );
            _effect = L1NpcXmlParser.getBoolAttribute( (string)attribute["Effect"], true );
        }

        override public L1NpcHtml execute( String actionName, L1PcInstance pc, L1Object obj, byte[] args )
        {
            if ( !pc.getInventory().checkItem( L1ItemId.ADENA, _price ) )
            {
                pc.sendPackets( new S_ServerMessage( 337, "$4" ) ); // アデナが不足しています。
                return L1NpcHtml.HTML_CLOSE;
            }
            pc.getInventory().consumeItem( L1ItemId.ADENA, _price );
            L1Teleport.teleport( pc, _loc.getX(), _loc.getY(), (short)_loc.getMapId(), _heading, _effect );
            return null;
        }
    }
}